package com.xhd.base.util;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

import com.xhd.db.BizObjFields;
import com.xhd.db.BizObjPK;
import com.xhd.db.DBControl;
import com.xhd.db.DBSQL;
import com.xhd.db.DBWhereExp;
import com.xhd.exception.DatabaseException;
import com.xhd.exception.IllegalDataException;
import com.xhd.exception.XException;
import com.xhd.server.ServerConfig;
import com.xhd.util.DataFormat;

public class XDataLang {
	private xBaseObj jm114 = null;
	  private Map jm483 = null;

	  public static boolean isMultiLang(xBaseObj paramxBaseObj)
	  {
	    String str = paramxBaseObj.getClassName().toLowerCase();
	    return (ServerConfig.dataPool.getMap("cpcobjlang", str) != null);
	  }

	  public XDataLang(xBaseObj paramxBaseObj)
	  {
	    this.jm114 = paramxBaseObj;
	    String str = paramxBaseObj.getClassName().toLowerCase();
	    this.jm483 = ServerConfig.dataPool.getMap("cpcobjlang", str);
	  }

	  public boolean isDefaultLang()
	  {
	    int i = Integer.parseInt((String)this.jm483.get("deflangid"));
	    return (this.jm114.getDataLangId() == i);
	  }

	  public String getPropNames()
	  {
	    return ((String)this.jm483.get("propnames"));
	  }

	  public boolean readData(int paramInt)
	    throws XException
	  {
	    if (paramInt == 0)
	      paramInt = this.jm114.getUserInfo().getLangId();
	    xBaseObj localxBaseObj = this.jm114;
	    DBSQL localDBSQL = new DBSQL();
	    localDBSQL.append("SELECT ");
	    localDBSQL.append((String)this.jm483.get("propnames"));
	    localDBSQL.append(" FROM ");
	    localDBSQL.append((String)this.jm483.get("objname"));
	    localDBSQL.append("_lang");
	    localDBSQL.append(" WHERE ");
	    localDBSQL.append(localxBaseObj.getDao().getDBType(), BizObjPK.getDBObjPK(localxBaseObj).getWhereExp(localxBaseObj));
	    localDBSQL.append(" AND langid=?");
	    localDBSQL.addInt(paramInt);
	    Hashtable localHashtable = localxBaseObj.getDao().oneRowSelect(localDBSQL);
	    if ((localHashtable != null) && (localHashtable.size() > 0))
	    {
	      localxBaseObj.toObject(localHashtable);
	      return true;
	    }
	    return false;
	  }

	  public boolean readData(Map paramMap, int paramInt)
	    throws XException
	  {
	    if (paramInt == 0)
	      paramInt = this.jm114.getUserInfo().getLangId();
	    xBaseObj localxBaseObj = this.jm114;
	    DBSQL localDBSQL = new DBSQL();
	    localDBSQL.append("SELECT ");
	    localDBSQL.append((String)this.jm483.get("propnames"));
	    localDBSQL.append(" FROM ");
	    localDBSQL.append((String)this.jm483.get("objname"));
	    localDBSQL.append("_lang");
	    localDBSQL.append(" WHERE ");
	    localDBSQL.append(localxBaseObj.getDao().getDBType(), BizObjPK.getDBObjPK(localxBaseObj).getWhereExp(paramMap));
	    localDBSQL.append(" AND langid=?");
	    localDBSQL.addInt(paramInt);
	    Hashtable localHashtable = localxBaseObj.getDao().oneRowSelect(localDBSQL);
	    if ((localHashtable != null) && (localHashtable.size() > 0))
	    {
	      localxBaseObj.toObject(localHashtable);
	      return true;
	    }
	    return false;
	  }

	  public void writeData(int paramInt, boolean paramBoolean)
	    throws DatabaseException
	  {
	    if (paramInt == 0)
	      paramInt = this.jm114.getUserInfo().getLangId();
	    xBaseObj localxBaseObj = this.jm114;
	    Hashtable localHashtable = localxBaseObj.toHashtable((String)this.jm483.get("propnames"));
	    String str = localxBaseObj.getClassName() + "_lang";
	    if (!(paramBoolean))
	    {
	    	DBWhereExp   localObject = BizObjPK.getDBObjPK(localxBaseObj).getWhereExp(localxBaseObj);
	      ((DBWhereExp)localObject).and(DBWhereExp.eq("langid", paramInt));
	      if (localxBaseObj.getDao().update(str, localHashtable, (DBWhereExp)localObject) == 0)
	        paramBoolean = true;
	    }
	    if (!(paramBoolean))
	      return;
	    Object localObject = BizObjPK.getDBObjPK(localxBaseObj).getPKField();
	    localHashtable.putAll(localxBaseObj.toHashtable((String)localObject));
	    localxBaseObj.getDao().insert(str, localHashtable);
	  }

	  public void writeData(Map paramMap, int paramInt, boolean paramBoolean)
	    throws DatabaseException
	  {
	    if (paramInt == 0)
	      paramInt = this.jm114.getUserInfo().getLangId();
	    String[] arrayOfString = DataFormat.splitString((String)this.jm483.get("propnames"), ",");
	    Hashtable localHashtable = new Hashtable(arrayOfString.length);
	    Object localObject;
	    for (int i = 0; i < arrayOfString.length; ++i)
	    {
	      localObject = paramMap.get(arrayOfString[i]);
	      if (localObject == null)
	        continue;
	      localHashtable.put(arrayOfString[i], localObject);
	    }
	    String str = this.jm114.getClassName() + "_lang";
	    if (!(paramBoolean))
	    {
	      localObject = BizObjPK.getDBObjPK(this.jm114).getWhereExp(this.jm114);
	      ((DBWhereExp)localObject).and(DBWhereExp.eq("langid", paramInt));
	      if (this.jm114.getDao().update(str, localHashtable, (DBWhereExp)localObject) == 0)
	        paramBoolean = true;
	    }
	    if (!(paramBoolean))
	      return;
	    this.jm114.getDao().insert(str, localHashtable);
	  }

	  public void onSelect(DBControl paramDBControl, DBWhereExp paramDBWhereExp)
	    throws DatabaseException
	  {
	    xBaseObj localxBaseObj = this.jm114;
	    DBSQL localDBSQL = new DBSQL();
	    localDBSQL.append("SELECT ");
	    localDBSQL.append((String)this.jm483.get("propnames"));
	    localDBSQL.append(" FROM ");
	    localDBSQL.append((String)this.jm483.get("objname"));
	    localDBSQL.append("_lang");
	    localDBSQL.append(" WHERE ");
	    localDBSQL.append(paramDBControl.getDBType(), BizObjPK.getDBObjPK(localxBaseObj).getWhereExp(localxBaseObj));
	    localDBSQL.append(" AND langid=?");
	    localDBSQL.addInt(this.jm114.getDataLangId());
	    Hashtable localHashtable = paramDBControl.oneRowSelect(localDBSQL);
	    if ((localHashtable == null) || (localHashtable.size() <= 0))
	      return;
	    try
	    {
	      this.jm114.toObject(localHashtable);
	    }
	    catch (IllegalDataException localIllegalDataException)
	    {
	      throw new DatabaseException(localIllegalDataException);
	    }
	  }

	  public void onDelete(DBControl paramDBControl, DBWhereExp paramDBWhereExp)
	    throws DatabaseException
	  {
	    DBSQL localDBSQL = new DBSQL();
	    localDBSQL.append("DELETE FROM ");
	    localDBSQL.append((String)this.jm483.get("objname"));
	    localDBSQL.append("_lang");
	    localDBSQL.append(" WHERE ");
	    localDBSQL.append(paramDBControl.getDBType(), paramDBWhereExp);
	    paramDBControl.execute(localDBSQL);
	  }

	  public void onInsert(DBControl paramDBControl, Map paramMap, BizObjFields paramBizObjFields)
	    throws DatabaseException
	  {
	    xBaseObj localxBaseObj = this.jm114;
	    Hashtable localHashtable = null;
	    String str1 = BizObjPK.getDBObjPK(localxBaseObj).getPKField().toLowerCase() + "," + ((String)this.jm483.get("propnames"));
	    if (paramMap == null)
	    {
	      localHashtable = localxBaseObj.toHashtable(str1);
	    }
	    else
	    {
	     String[]   localObject1 = DataFormat.splitString(str1, ",");
	      localHashtable = new Hashtable(localObject1.length);
	      for (int i = 0; i < localObject1.length; ++i)
	      {
	       Object localObject2 = paramMap.get(localObject1[i]);
	        if (localObject2 == null)
	          continue;
	        localHashtable.put(localObject1[i], localObject2);
	      }
	    }
	    Object localObject1 = new DBSQL();
	    StringBuffer localStringBuffer = new StringBuffer(256);
	    ((DBSQL)localObject1).append("INSERT INTO ");
	    ((DBSQL)localObject1).append(localxBaseObj.getClassName());
	    ((DBSQL)localObject1).append("_lang (");
	    localStringBuffer.append(") VALUES(");
	    Object localObject2 = localHashtable.keySet().iterator();
	    int j = 1;
	    while (((Iterator)localObject2).hasNext())
	    {
	      String str2 = (String)((Iterator)localObject2).next();
	      int k = paramBizObjFields.getJdbcType(str2);
	      if (k == 0)
	        continue;
	      if (j == 0)
	      {
	        ((DBSQL)localObject1).append(",");
	        localStringBuffer.append(',');
	      }
	      else
	      {
	        j = 0;
	      }
	      ((DBSQL)localObject1).append(str2);
	      localStringBuffer.append('?');
	      ((DBSQL)localObject1).addParam(k, (String)localHashtable.get(str2));
	    }
	    if (j != 0)
	      return;
	    ((DBSQL)localObject1).append(",langid");
	    localStringBuffer.append(",?");
	    localStringBuffer.append(')');
	    ((DBSQL)localObject1).addInt(this.jm114.getDataLangId());
	    ((DBSQL)localObject1).append(localStringBuffer.toString());
	    paramDBControl.execute((DBSQL)localObject1);
	  }

	  public void onUpdate(DBControl paramDBControl, DBWhereExp paramDBWhereExp, BizObjFields paramBizObjFields1, BizObjFields paramBizObjFields2)
	    throws DatabaseException
	  {
	    String str1 = (String)this.jm483.get("propnames");
	    String[] arrayOfString = DataFormat.splitString(str1, ",");
	    Hashtable localHashtable = this.jm114.toHashtable(str1);
	    int i = Integer.parseInt((String)this.jm483.get("deflangid"));
	    int j = (this.jm114.getDataLangId() == i) ? 1 : 0;
	    for (int k = 0; k < arrayOfString.length; ++k)
	      if (!(paramBizObjFields1.hasField(arrayOfString[k])))
	      {
	        localHashtable.remove(arrayOfString[k]);
	      }
	      else
	      {
	        if (j != 0)
	          continue;
	        paramBizObjFields1.remove(arrayOfString[k]);
	      }
	    DBSQL localDBSQL = new DBSQL();
	    localDBSQL.append("UPDATE ");
	    localDBSQL.append(this.jm114.getClassName());
	    localDBSQL.append("_lang SET ");
	    int l = paramDBControl.getDBType();
	    int i1 = 1;
	    Iterator localIterator = localHashtable.keySet().iterator();
	    while (localIterator.hasNext())
	    {
	      String str2 = (String)localIterator.next();
	      if (!(localHashtable.containsKey(str2)))
	        continue;
	      int i2 = paramBizObjFields2.getJdbcType(str2);
	      if (i2 == 0)
	        continue;
	      if (i1 == 0)
	        localDBSQL.append(",");
	      else
	        i1 = 0;
	      localDBSQL.append(str2);
	      localDBSQL.append("=?");
	      localDBSQL.addParam(i2, (String)localHashtable.get(str2));
	    }
	    if (i1 != 0)
	      return;
	    localDBSQL.append(" WHERE ");
	    localDBSQL.append(l, paramDBWhereExp);
	    localDBSQL.append(" AND langid=?");
	    localDBSQL.addInt(this.jm114.getDataLangId());
	    if (paramDBControl.execute(localDBSQL) != 0)
	      return;
	    onInsert(paramDBControl, null, paramBizObjFields2);
	  }
}
